package education.dao.impl;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Component;

import education.dao.ArticleDao;
import education.exception.DAOException;
import education.model.Admin;
import education.model.Article;
import education.model.Course;

@Component("articleDao")
public class ArticleDaoImpl implements ArticleDao {

	private HibernateTemplate hibernateTemplate;

	public void delete(Article article) {

	}

	public boolean delete(int id) {
		Session session = hibernateTemplate.getSessionFactory().openSession();
		Query query = session
				.createQuery("delete Article article where article.id=?");
		query.setInteger(0, id);
		int re = query.executeUpdate();
		session.close();
		if (re != 0) {
			return true;
		}
		return false;
	}

	public int getCount() {
		int count = Integer.parseInt(hibernateTemplate.find(
				"select count(*) from Article").listIterator().next()
				.toString());
		return count;
	}

	public HibernateTemplate getHibernateTemplate() {
		return hibernateTemplate;
	}

	public Article loadById(int id) {
		Session s = hibernateTemplate.getSessionFactory().getCurrentSession();
		return (Article) s.get(Article.class, id);
	}

	public List<Article> paging(int pageSize, int pageCount, int rowCount) {
		List<Article> list;
		pageSize = (pageSize - 1) * rowCount;
		Session session = hibernateTemplate.getSessionFactory()
				.getCurrentSession();
		Query query = session.createQuery("from Article").setFirstResult(
				pageSize).setMaxResults(rowCount);
		list = query.list();
		return list;
	}

	public void save(Article article) {
		hibernateTemplate.save(article);
	}

	@Resource
	public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
		this.hibernateTemplate = hibernateTemplate;
	}

	public boolean update(Article article) {
		hibernateTemplate.update(article);
		return true;
	}

	public List<Course> getCourseList() throws DAOException {
		// TODO Auto-generated method stub
		return null;
	}

}
